// pages/mine/members/members.js
const app = getApp(),
    globalData = app.globalData,
    api = require("../../../api/index"),
    testdata = require("testdata.js"),
    utils = require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        backImg:globalData.pictureRouting+'wodetuanbeibackground.png',
        canpIcon:globalData.pictureRouting+'touxiang_icon_wodetiuand.png',
        activeNames: ['0'],
        // membertype：1 初级,2 高级,4 院长
        userInfo: {},
        status: 0,
        toast: {
            visible: false,
            text: ''
        },
        imageBaseUri: globalData.imageBaseUri,
        array: ['所有等级'],
        index: 0,
        filter: {
            NickName: '',
            MemberType: '',
            CurrenetPageIndex: 1,
            PageSize: 10
        },
        loading: false,
        hasMore: false,
        userData: {},
        agentInfo: [],

        totalPages1: 0,
        totalPages2: 0,
        totalPages3: 0,
        currenetPageIndex1: 1,
        currenetPageIndex2: 1,
        currenetPageIndex3: 1,
        subordinateArr: [],
        withdrawalArr: [],
        fanliArr: []
    },
    changeActive(e){
        this.data.active = e.detail.index
    },
    onChange(event){
        console.log(event)
        // this.setData({
        //     activeNames: event.detail,
        //   });
          wx.navigateTo({
            url: '/pages/mine/teamDetails/index?userid=' + event.currentTarget.dataset.userid + '&agentInfo=' + JSON.stringify(this.data.agentInfo) + '&userInfo=' + JSON.stringify(this.data.userInfo),
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({agentInfo: JSON.parse(options.agentInfo)})
        this.setData({userInfo: JSON.parse(options.userInfo)})
        this.reload()
        console.log(this.data.agentInfo);
    },

    reload() {

        this.setData({
            userInfo: wx.getStorageSync('userinfo') || {}
        })
        
        if (this.data.userInfo.MemberGradeId == 1) {
            this.setData({
                array: ['所有等级', '产品顾问']
            })
        } else if (this.data.userInfo.MemberGradeId == 2) {
            this.setData({
                array: ['所有等级', '产品顾问', '高级产品顾问']
            })
        } else if (this.data.userInfo.MemberGradeId == 4) {
            this.setData({
                array: ['所有等级', '产品顾问', '高级产品顾问', '院长']
            })
        } else {
            this.setData({
                array: ['所有等级']
            })
        }

        // this.getList();
        // this.getMySales();
        this.getRebate()
        this.getSubordinate()
        this.getMarket()
        this.getWithdrawal()
    },

    // async getRebate(){
    //     let res = await api.member.getRebate()
    //     let data = res.data.ResData
    //     this.setData({
    //         fanliArr: data.Items
    //     })
        
    //     // this.setData({
    //     //     fanliArr: testdata.data.ResData.Items
    //     // })
    // },

    // async getSubordinate(){

    //     try{
    //         let res = await api.member.getSubordinate(this.data.filter)
    //         console.log(res)
    //         let data = res.data.ResData
    //         let list = data.Items || []
    //         // 多页
    //         if(this.data.filter.CurrenetPageIndex > 1){
    //             list = this.data.subordinateArr.concat(list)
    //         }

    //         this.setData({
    //             subordinateArr: list,
    //             hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
    //         })
    //     } catch (e){
    //         console.log(e, '获取我的下级列表失败')
    //     }

        
        

    //     // this.setData({
    //     //     subordinateArr: testdata.data2.ResData.Items
    //     // })
    // },

    async getMarket(){
        let res = await api.member.getMarket()
        let data = res.data.ResData
        this.setData({
            marketStr: data
        })
        // this.setData({
        //     marketStr: testdata.data3.ResData
        // })
    },

    // async getWithdrawal(){
    //     let res = await api.member.getWithdrawal()
    //     let data = res.data.ResData
    //     this.setData({
    //         withdrawalArr: data.Items
    //     })
    //     // this.setData({
    //     //     withdrawalArr: testdata.data4.ResData.Items
    //     // })
    // },

    /**
     * 页面上拉触底事件的处理函数
     */
    // onReachBottom: function () {
    //     console.log(this.data.hasMore, 'hasmore')
    //     if (this.data.hasMore) {
    //         this.setData({
    //             'filter.CurrenetPageIndex': ++this.data.filter.CurrenetPageIndex
    //         });
    //         this.getSubordinate()
    //         // this.getList();
    //     }
    // },

    async getRebate(){
        let res = await api.member.getRebate({
            page: this.data.currenetPageIndex1
        })
        this.setData({
            totalPages1: res.data.ResData.TotalPages,
            fanliArr: this.data.fanliArr.concat(res.data.ResData.Items)
        })
    },

    async getSubordinate(){
        let res = await api.member.getSubordinate({
            page: this.data.currenetPageIndex2
        })
        this.setData({
            totalPages2: res.data.ResData.TotalPages,
            subordinateArr: this.data.subordinateArr.concat(res.data.ResData.Items)
        })
    },

    async getWithdrawal(){
        let res = await api.member.getWithdrawal({
            page: this.data.currenetPageIndex3,
        })
        this.setData({
            totalPages3: res.data.ResData.TotalPages,
            withdrawalArr: this.data.withdrawalArr.concat(res.data.ResData.Items)
        })
    },

     /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.active==0){
            if(this.data.currenetPageIndex1<this.data.totalPages1){
                this.data.currenetPageIndex1++
                this.getRebate()
            }
        }else if(this.data.active==1){
            if(this.data.currenetPageIndex2<this.data.totalPages2){
                this.data.currenetPageIndex2++
                this.getSubordinate()
            }
        }else if(this.data.active==3){
            if(this.data.currenetPageIndex3<this.data.totalPages3){
                this.data.currenetPageIndex3++
                this.getWithdrawal()
            }
        }
    },

    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value
        })
        switch (this.data.array[this.data.index]) {
            case '产品顾问':
                this.setData({
                    'filter.MemberType': 1,
                    "filter.CurrenetPageIndex": 1
                })
                this.getList();
                break;
            case '高级产品顾问':
                this.setData({
                    'filter.MemberType': 2,
                    "filter.CurrenetPageIndex": 1
                })
                this.getList();
                break;
            case '院长':
                this.setData({
                    'filter.MemberType': 4,
                    "filter.CurrenetPageIndex": 1
                })
                this.getList();
                break;
            default:
                this.setData({
                    'filter.MemberType': '',
                     "filter.CurrenetPageIndex": 1
                })
                this.getList();
                return;
        }
        
    },

    // 获取所属会员列表
    async getList() {
        try {
            let res = await api.member.getChilds(this.data.filter),
                data = res.data.ResData || {},
                list = data.Items || [];
            console.log(res, '所属会员列表')

            //多页
            if (this.data.filter.CurrenetPageIndex > 1) {
                list = this.data.list.concat(list);
            }

            this.setData({
                status: 2,
                list,
                loading: false,
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
            })
        } catch (e) {
            console.log(e, '获取所属会员列表失败')
            this.setData({
                status: 1
            })
        }
    },

    // 获取本人的销售额
    async getMySales() {
        try {
            let res = await api.member.getSales();
            console.log(res,'本人销售额')
            this.setData({
                userData: res.data.ResData || {}
            })
        }catch(e){
            console.log(e,'获取本人销售额失败')
        }
    },

    // 输入
    getInput(e){
        console.log(e,'e')
        this.setData({
           "filter.NickName": e.detail.value
        })
    },
    search(){
        this.getList();
    },
    tixian(){
        wx.navigateTo({
          url: '/pages/mine/withdrawalp/withdrawalp',
        })
    }
})